package com.ldx;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lidexin
 * @create 2021-09-20 14:05
 */
public class SaleTicketDemo02 {

    public static void main(String[] args) {
        //并发，多线程操作同一个资源类，把资源类丢进线程
        Ticket2 ticket = new Ticket2();
        new Thread(()->{
            for (int i = 1; i < 40; i++) {
                ticket.sale();
            }
        },"A").start();

        new Thread(()-> {
            for (int i = 1; i < 40; i++) {
                ticket.sale();
            }
        },"B").start();

        new Thread(()-> {
            for (int i = 1; i < 40; i++) {
                ticket.sale();
            }
        },"C").start();
    }
}


//Lock 三部曲
//1、new ReentrantLock()
//2、lock.lock()；//加锁
//3、finally => lock.unlock(); 解锁
class Ticket2{
    //属性、方法
    private int number = 30;

    Lock lock = new ReentrantLock();

    public  void sale() {
        lock.lock();//加锁
        try {
            //业务代码
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "剩余：" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//解锁
        }
    }
}


